// JavaScript Document
function createRequest() 
{
  try 
  {
    request = new XMLHttpRequest();
  } 
  catch (trymicrosoft) 
  {
    try 
	{
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (othermicrosoft) 
	{
      try 
	  {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } 
	  catch (failed) 
	  {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");
}

function createRequests() 
{
   try 
  {
      this.req = new XMLHttpRequest();
  } 
  catch (trymicrosoft) 
  {
    try 
	{
      this.req = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (othermicrosoft) 
	{
      try 
	  {
        this.req = new ActiveXObject("Microsoft.XMLHTTP");
      } 
	  catch (failed) 
	  {
        this.req = false;
      }
    }
  }

  if (!this.req)
    alert("Error initializing XMLHttpRequest!");
}

//XMLHTTPREQUEST using post array
var http_request = false;
function createPOSTRequest()
{
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
         // set type accordingly to anticipated content type
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   
}

function savefile(path_to_file, content, controller) 
{
   var parameters = "content=" + encodeURIComponent(content) + "&path_to_file=" + encodeURI(path_to_file);
   createPOSTRequest();
 
   http_request.onreadystatechange = function()
   {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
            document.getElementById('frame_file_source').contentDocument.getElementById('statusMessage').innerHTML = 'File saved';            
         } else {
            document.getElementById('frame_file_source').contentDocument.getElementById('statusMessage').innerHTML = 'File NOT saved';
         }
      }   
   }
   http_request.open('POST', '/spanel/' + controller + '/savefile/', true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
 
   document.getElementById('frame_file_source').contentDocument.getElementById('statusMessage').innerHTML = 'File saved';
   
}

function SendValues(url)
{
	createRequest();
	var url = url;
	request.open("GET", url, true);
	request.onreadystatechange = null;
	request.send(null);	
}

function updatefileexplorer()
{
   createPOSTRequest();
 
   http_request.onreadystatechange = function()
   {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText.split("@");
            alert(result[1]);
            document.getElementById('file_explorer_div').innerHTML = result[1];            
         }
      }   
   }
   http_request.open('POST', '/spanel/main/UpdateFileExplorerMenu/', true);
   http_request.send(null); 
}

function RefreshSession()
{
	createRequest();
	var url = url;
	request.open("GET", '/spanel/main/refreshsession/', true);
    request.onreadystatechange = function()
    {
      if (request.readyState == 4) {
         if (request.status == 200) {
			window.location.replace(location.href.replace('#', ''));
         }
      }   
    }
	request.send(null);	
}

function SaveProjectExplorer(content, link)
{
   var parameters = "content=" + encodeURIComponent(content);
   createPOSTRequest();
 
   http_request.onreadystatechange = function()
   {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
			window.location.replace(link.href);
         }
      }   
   }

   http_request.open('POST', '/spanel/main/SaveProjectExplorer/', true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
 
   //document.getElementById('frame_file_source').contentDocument.getElementById('statusMessage').innerHTML = 'File saved';

}

function GetHtmlElement(url,id)
{
	    var request = new Array()
		request[id] = new createRequests();
		request[id].req.onreadystatechange = function()
		{
		if (request[id].req.readyState == 4)
	 		if (request[id].req.status == 200) 
			{
				response = request[id].req.responseText.split("@");
			    document.getElementById(id).parentNode.innerHTML = response[1];
			}
		}
		request[id].req.open("GET", url, true);
		request[id].req.send(null);
}